import pako from './pako.es5.min.js'

//解密
class Pkao {
	constructor(handler) {

	}

	//解密
	deCompress(str) {
		return pako.inflateRaw(this.base64ToUint8Array(str), {
			to: 'string'
		});
	}

	base64ToUint8Array(base64String) {
		let padding = '='.repeat((4 - base64String.length % 4) % 4);
		let base64 = (base64String + padding)
			.replace(/\-/g, '+')
			.replace(/_/g, '/');
		let rawData = this.atob(base64);
		let outputArray = new Uint8Array(rawData.length);
		for (var i = 0; i < rawData.length; ++i) {
			outputArray[i] = rawData.charCodeAt(i);
		}
		return outputArray;
	}

	atob(input) {
		const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
		let str = input.replace(/=+$/, '');
		let output = '';
		if (str.length % 4 === 1) {
			throw new Error('InvalidLengthError');
		}
		for (let i = 0, len = str.length; i < len; i += 4) {
			const a = chars.indexOf(str.charAt(i));
			const b = chars.indexOf(str.charAt(i + 1));
			const c = chars.indexOf(str.charAt(i + 2));
			const d = chars.indexOf(str.charAt(i + 3));
			const sum = (a << 18) | (b << 12) | (c << 6) | d;
			output += String.fromCharCode((sum >> 16) & 0xFF, (sum >> 8) & 0xFF, sum & 0xFF);
		}
		return output;
	}
}

export default new Pkao;